package com.mingqijia.gassafety.job.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.db.entity.Consumer;
import com.mingqijia.gassafety.db.entity.WorkOrderHistory;
import com.mingqijia.gassafety.job.request.BindingPositionRequest;
import com.mingqijia.gassafety.job.request.ConsumerBindingRequest;
import com.mingqijia.gassafety.job.service.ConsumerService;
import com.mingqijia.gassafety.job.service.MbpWorkOrderService;
import com.mingqijia.gassafety.job.service.WorkOrderHistoryService;
import com.mingqijia.gassafety.job.service.mbp.TcisService;
import com.mingqijia.gassafety.shared.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: gassafety
 * @description:
 * @author: Mr.Wang
 * @create: 2022-12-06 14:25
 **/
@Slf4j
@Service
public class MbpWorkOrderServiceImpl implements MbpWorkOrderService {
    @Autowired
    ConsumerService consumerService;
    @Autowired
    TcisService tcisService;
    @Autowired
    WorkOrderHistoryService workOrderHistoryService;

    @Override
    public void queryAlarmAppliance(WorkOrderHistory history) {
        String subsCode = history.getSubsCode();
        String spId = history.getSpId();
        String pkid = history.getPkid();
        // 查询所有 没有受理的诉求, 进行操作
//        ** 必要条件: 只要3016接口不成功,就不允许继续5016发起诉求
        String param = subsCode;
        log.info("queryAlarmAppliance-调用 1022 报警器信息记录查询");
        Map<String, Object> alarmApplianceMap = tcisService.tcisRequest(null, 3, param, spId,subsCode);
        Integer code2 = (Integer) alarmApplianceMap.get("code");
        if (!code2.equals(0)) throw new BizException("1022 报警器信息记录查询调用失败");
        JSONArray alarmList = JSONArray.parseArray(JSON.toJSONString(alarmApplianceMap.get("data")));
        log.info(JSONObject.toJSONString(alarmList));
        for (int i = 0; i < alarmList.size(); i++) {
            JSONObject alarm = alarmList.getJSONObject(i);
            // pkid对应,进行绑定
            if (alarm.getString("pkid").equals(pkid)) {
                QueryWrapper<Consumer> consumerQueryWrapper = new QueryWrapper<>();
                consumerQueryWrapper.lambda().eq(Consumer::getSubsCode, subsCode)
                        .eq(Consumer::getSpId, spId)
                        .eq(Consumer::getIsDeleted, Constants.IS_DELETED_FALSE);
                Consumer consumer = consumerService.getOne(consumerQueryWrapper);
                ConsumerBindingRequest request = new ConsumerBindingRequest();
                BindingPositionRequest positionRequest = new BindingPositionRequest();
                String imei = alarm.getString("resid");
                positionRequest.setImei(imei);
                List<BindingPositionRequest> list = new ArrayList<>();
                list.add(positionRequest);
                request.setBindChannel(Constants.BIND_CHANNEL_MBP);
                request.setImeiList(list);
                request.setConsumerId(consumer.getId());
                request.setSpId(spId);


                //根据 安装单号更新设备信息
                WorkOrderHistory workOrderHistory = new WorkOrderHistory();
                workOrderHistory.setId(history.getId());
                workOrderHistory.setDeviceId(imei);
                workOrderHistoryService.updateById(workOrderHistory);
                consumerService.bindingEquipment(request);

            }
        }
    }
}
